#ifndef BSP__OLED_H__
#define BSP__OLED_H__
#include "at32f435_437.h"

#ifdef __cplusplus
extern "C" {
#endif




/* OLED spi */
#define OLED_SPI_ID						SPI2
#define OLED_SPI_CRM_CLK				CRM_SPI2_PERIPH_CLOCK		
/* cs */		
#define OLED_SPI_CS_CRM					CRM_GPIOA_PERIPH_CLOCK
#define OLED_SPI_CS_PIN					GPIO_PINS_11
#define OLED_SPI_CS_GPIO				GPIOA
#define OLED_SPI_CS_SOURCE				GPIO_PINS_SOURCE11
#define OLED_SPI_CS_MUX					GPIO_MUX_5
/* sck */		
#define OLED_SPI_SCK_CRM				CRM_GPIOC_PERIPH_CLOCK
#define OLED_SPI_SCK_PIN				GPIO_PINS_9
#define OLED_SPI_SCK_GPIO				GPIOA
#define OLED_SPI_SCK_SOURCE				GPIO_PINS_SOURCE9
#define OLED_SPI_SCK_MUX				GPIO_MUX_5
/* miso */		
//#define OLED_SPI_MISO_CRM				CRM_GPIOA_PERIPH_CLOCK
//#define OLED_SPI_MISO_PIN				GPIO_PINS_12
//#define OLED_SPI_MISO_GPIO				GPIOA
//#define OLED_SPI_MISO_SOURCE			GPIO_PINS_SOURCE12
//#define OLED_SPI_MISO_MUX				GPIO_MUX_5
/* mosi */		
#define OLED_SPI_MOSI_CRM				CRM_GPIOA_PERIPH_CLOCK
#define OLED_SPI_MOSI_PIN				GPIO_PINS_10
#define OLED_SPI_MOSI_GPIO				GPIOA
#define OLED_SPI_MOSI_SOURCE			GPIO_PINS_SOURCE10
#define OLED_SPI_MOSI_MUX				GPIO_MUX_5
/* DC */
//#define OLED_SPI_DC_PIN					GPIO_PINS_9
//#define OLED_SPI_DC_PORT        		GPIOC
//#define OLED_SPI_DC_CRM_CLK     		CRM_GPIOC_PERIPH_CLOCK
#define OLED_SPI_DC_PIN					GPIO_PINS_11
#define OLED_SPI_DC_PORT        		GPIOA
#define OLED_SPI_DC_CRM_CLK     		CRM_GPIOA_PERIPH_CLOCK

/* RES */
//#define OLED_SPI_RES_PIN				GPIO_PINS_8
//#define OLED_SPI_RES_PORT       		GPIOA
//#define OLED_SPI_RES_CRM_CLK    		CRM_GPIOA_PERIPH_CLOCK
#define OLED_SPI_RES_PIN				GPIO_PINS_12
#define OLED_SPI_RES_PORT       		GPIOA
#define OLED_SPI_RES_CRM_CLK    		CRM_GPIOA_PERIPH_CLOCK


/* 中断 */		
#define OLED_SPI_IRQn					SPI2_I2S2EXT_IRQn
#define OLED_SPI_IRQHandler				SPI2_I2S2EXT_IRQHandler
/* dma */		
#define OLED_SPI_DMA_ID					DMA2
#define OLED_SPI_DMA_CRM_CLK			CRM_DMA2_PERIPH_CLOCK
#define OLED_SPI_DMA_TX_CHANNEL			DMA2_CHANNEL2
#define OLED_SPI_DMA_TX_MUX				DMA2MUX_CHANNEL2
#define	OLED_SPI_DMA_TX_MUX_ID			DMAMUX_DMAREQ_ID_SPI2_TX
#define OLED_SPI_DMA_IRQn				DMA2_Channel2_IRQn
#define OLED_SPI_DMA_IRQHandler			DMA2_Channel2_IRQHandler

#define OLED_RES_Clr()					do{gpio_bits_write(OLED_SPI_RES_PORT, OLED_SPI_RES_PIN, FALSE);}while(0)
#define OLED_RES_Set()					do{gpio_bits_write(OLED_SPI_RES_PORT, OLED_SPI_RES_PIN, TRUE) ;}while(0)

#define OLED_DC_Clr()					do{gpio_bits_write(OLED_SPI_DC_PORT, OLED_SPI_DC_PIN,   FALSE);}while(0)
#define OLED_DC_Set()					do{gpio_bits_write(OLED_SPI_DC_PORT, OLED_SPI_DC_PIN,   TRUE) ;}while(0)

#define OLED_CS_Clr()					do{gpio_bits_write(OLED_SPI_CS_GPIO, OLED_SPI_CS_PIN,   FALSE);}while(0)
#define OLED_CS_Set()					do{gpio_bits_write(OLED_SPI_CS_GPIO, OLED_SPI_CS_PIN,   TRUE) ;}while(0)

/* 操作类型 */
#define OLED_CMD  0	//写命令
#define OLED_DATA 1	//写数据

// olcd 屏幕尺寸
#define OLCD_WIDTH		128	//oled显示宽度
#define OLCD_HIGH		64	//oled显示高度


// OLED驱动函数
void oledInit(void);
void oled_clear(void);
void oled_refresh_gram(void);
void oled_draw_point(u8 x, u8 y, u8 t);
u8 	 oled_readPoint(u8 x, u8 y);
void oled_fill(u8 x1, u8 y1, u8 x2, u8 y2, u8 dot);
void oled_show_char(u8 x, u8 y, u8 chr, u8 f_w, u8 f_h, u8 mode);
void oled_show_num(u8 x, u8 y, u32 num, u8 len, u8 f_w, u8 f_h, u8 mode);
void oled_show_string(u8 x, u8 y, const u8 *p, u8 f_w, u8 f_h, u8 mode);
void oled_show_picture(u8 x, u8 y, const u8 *p, u8 p_w, u8 p_h);
void show_font(u16 x, u16 y, const u8 *font, u8 f_w, u8 f_h, u8 mode);
void show_str(u16 x, u16 y, const u8* str, u8 f_w, u8 f_h, u8 mode);
void show_str_mid(u16 x, u16 y, const u8* str, u8 f_w, u8 f_h, u8 mode, u8 len);


void bsp_oled_init(void);
void oled_spi_send_data(uint8_t *buff, uint16_t len);



#ifdef __cplusplus
}
#endif

#endif

